import {devices,screencap,pullPng,deletePhoneImg} from '../../../utils/AdbProxy';
import {call} from '../../../utils/DMPlugin';
import uuid from 'node-uuid';
var fs = require("fs");
var path = require("path");

const pullQueue = [];//要刪除的隊列
const deleteQueue = []; //要拉取的图片队列
const localImgQueue = []; //本地图片队列

export default {
    state: {
        version: '1.0',
        user: {
            name: 'alvin',
            theme: 0
        },

        //
        devices: [],
        currentDevice: {},//当前设备信息
        currentImg: null,//要显示的图 


    },
    getters: {
        // ["app/_getCurrDevice"](state){
        //     return state.currentDevice;
        // },
        // ["app/_getCurrImg"](state){
        //     return state.currentImg;
        // },
    },
    mutations: {
        ['app/setDevices'](state,devices){
             state.devices = devices;
        },
        ["app/changeCurrentPhone"](state,data){ 
            state.currentDevice = data;
        },
        ["app/setCurrentImg"](state,currImg){
            state.currentImg = currImg;
        },
        

    },
    actions: {
        ["app/scanDevices"]({state, dispatch, commit}){
            //3秒執行一次掃描操作
            setInterval(function(){
                console.log("三秒执行一次扫描")
                devices().then(devices =>{
                    commit("app/setDevices",devices)
                    devices.map(item =>{
                        //一个设备一个目录
                        if(!fs.existsSync(item.label)){
                            fs.mkdirSync(item.label); 
                        } 
                    })
                }).catch(err =>{
                    console.log("扫描设备出错：",err);
                });
            },3000)
             
            dispatch("app/run");
            dispatch("app/pull");
           
                dispatch("app/phoneRmFile");
                dispatch("app/localImgRm");
           
        },
        ["app/run"]({state, dispatch, commit}){
            //定时截图
            setInterval(function(){
                if(state.devices.length === 0){
                    return;
                }  
            const pngPath = uuid.v1() +".png";
            for(let i = 0 ; i < state.devices.length;i++){
                const sid = state.devices[i].label;
                screencap(sid,pngPath).then(res =>{
                     //进入数据拉去队列
                     pullQueue.push( {
                        sid: sid,
                        pngPath: pngPath
                    }) 
                }).catch(err =>{
                    console.log("截屏出错",err,sid,pngPath);
                });
            }
            },1000 /2 )
        },
        ["app/pull"]({state, dispatch, commit}){
            //拉取图片
           setInterval(function(){
            if( pullQueue.length === 0){
                return ;
               }
               let pullTask = pullQueue.pop();
            //   debugger
               pullPng(pullTask.sid,pullTask.pngPath).then(res =>{
                   console.log("拉取图片成功",pullTask);
                //   debugger
                   if(state.currentDevice){
                        if( state.currentDevice.label === pullTask.sid){
                            commit("app/setCurrentImg",pullTask.sid+"/"+pullTask.pngPath);
                    }
                   }
                  
                //    call(pullTask.sid,pullTask.pngPath).then(res =>{ 
                //     fs.unlinkSync("./"+pullTask.sid+"/"+pullTask.pngPath);
                //    }).catch(err =>{
                //     fs.unlinkSync("./"+pullTask.sid+"/"+pullTask.pngPath);
                //    });
                   deletePhoneImg(pullTask); //拉取成功的，进入删除队列
               }).catch(err =>{
                   deletePhoneImg(pullTask);//没有拉取的，加入删除队列
               })
           },400)
            
        },
        ["app/phoneRmFile"]({state, dispatch, commit}){
         
                
        },
        ["app/localImgRm"]({state, dispatch, commit}){
         
        }
    }
};